// addrspace.h 
//	Data structures to keep track of executing user programs 
//	(address spaces).
//
//	For now, we don't keep any information about address spaces.
//	The user level CPU state is saved and restored in the thread
//	executing the user program (see thread.h).
//
// Copyright (c) 1992-1996 The Regents of the University of California.
// All rights reserved.  See copyright.h for copyright notice and limitation 
// of liability and disclaimer of warranty provisions.

#ifndef ADDRSPACE_H
#define ADDRSPACE_H

#include "copyright.h"
#include "filesys.h"

#include "machine.h"

#define UserStackSize		1024 	// increase this as necessary!

enum TranslationStatus {
	OK,
	PageFault,
	ProtectionError,
	BusError,          // invalid physical address
	AddressError       // invalid virtual address
};

#define MaxOpenFiles        32
#define	MaxStringArgLength	256

class AddrSpace {
  public:
    AddrSpace();			// Create an address space.
    ~AddrSpace();			// De-allocate an address space

    void Execute(char *fileName, int argc, char* argv[]);	// Run the the program stored in the file "fileName"
    void Execute(char *fileName);	// Run the the program stored in the file "fileName"

    void SaveState();			// Save/restore address space-specific
    void RestoreState();		// info on a context switch 
    
    TranslationStatus Translate(int vaddr,int *paddr, bool write);
    TranslationStatus ReadMem(int addr, int size, int *value);  // read from virtual address
    TranslationStatus WriteMem(int addr, int size, int value); // write to virtual address
    
    int UserStringToKernel(int vaddr, char* buf);    
	int UserBufToKernel(int vaddr, int length, char* buf);
	int KernelToUserBuf(int vaddr, int length, char* buf);

	void PrintInfo();
	 
  private:
    TranslationEntry *pageTable;	// Assume linear page table translation
					// for now!
    unsigned int numPages;		// Number of pages in the virtual 
					// address space

    bool Load(char *fileName);		// Load the program into memory
					// return false if not found

    void InitRegisters();		// Initialize user-level CPU registers,
					// before jumping to user code

    int stackPage;
    int registers[NumTotalRegs];
    
    int id;
    static int spaces;
};

#endif // ADDRSPACE_H
